//
//  QZHongBaoSignView.m
//  shortvideoplay
//
//  Created by adam on 2024/7/30.
//

#import "QZHongBaoSignView.h"
#import "QZHomeBaoSignCell.h"
#import "QZHomeBaoSignModel.h"
#import "CSJADLoading.h"
@interface QZHongBaoSignView ()<UICollectionViewDataSource,
UICollectionViewDelegate,
UICollectionViewDelegateFlowLayout>
{
    UICollectionView *_collectionView;
    UILabel *signDayLabel;
}
@property (strong, nonatomic)UIView * adView;
@property (strong, nonatomic)NSArray * dataSource;
@property (assign, nonatomic)NSInteger mingtianDay;
@property (strong, nonatomic)UIButton * signButton;
@property (assign, nonatomic)BOOL isSignRequest;
@end
@implementation QZHongBaoSignView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = ColorHex_Alpha(@"000000", 0.45);
        
        
        
        UIView * showView = [[UIView alloc]initWithFrame:CGRectMake((KScreen_W-280)/2, (KScreen_H-310)/2-35, 280, 345)];
        showView.layer.cornerRadius = 15;
        showView.clipsToBounds = YES;
        showView.backgroundColor = subviewback_black;
        [self addSubview:showView];
        UIImageView * showImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 280, 400)];
        showImg.image = KImageNamed(@"ic_home_system_showBgImage");
        showImg.contentMode = UIViewContentModeScaleToFill;
        [showView addSubview:showImg];
        
        
        UIButton * closeButton = [[UIButton alloc]initWithFrame:CGRectMake((KScreen_W-40)/2, (KScreen_H-310)/2+365, 40, 40)];
        [closeButton addTarget:self action:@selector(closeTap) forControlEvents:UIControlEventTouchUpInside];
        closeButton.alpha = 0.62;
        [closeButton setImage:KImageNamed(@"ico_sign_close") forState:UIControlStateNormal];
        [self addSubview:closeButton];
    
        UILabel * titleLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 280, 50)];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = @"每日签到";
        titleLabel.font = FontBold(16);
        titleLabel.textColor = UIColor.whiteColor;
        [showView addSubview:titleLabel];
        
        UIView * signView = [[UIView alloc]initWithFrame:CGRectMake(10, 50, 260, 205)];
        signView.backgroundColor = UIColor.whiteColor;
        signView.layer.cornerRadius = 5;
        signView.clipsToBounds = YES;
        [showView addSubview:signView];
        
        signDayLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 260, 40)];
        signDayLabel.textAlignment = NSTextAlignmentCenter;
        signDayLabel.font = Font(13);
        signDayLabel.textColor = MainColor;
        [signView addSubview:signDayLabel];
        
        
        UICollectionViewFlowLayout *layout= [UICollectionViewFlowLayout new];
        layout.minimumLineSpacing =  0;
        layout.minimumInteritemSpacing = 0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(10, 40, 240, 165) collectionViewLayout:layout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        [signView addSubview:_collectionView];
        [_collectionView registerClass:[QZHomeBaoSignCell class] forCellWithReuseIdentifier:@"QZHomeBaoSignCell"];
      
        _signButton = [[UIButton alloc]initWithFrame:CGRectMake(30, 275, 220, 48)];
        _signButton.layer.cornerRadius = 20;
        _signButton.clipsToBounds = YES;
        [_signButton setBackgroundImage:[Tools changeImagSize:KImageNamed(@"mall_shop_buyBtn") toSize:CGSizeMake(220, 48)] forState:UIControlStateNormal];
        [_signButton setTitle:[NSString stringWithFormat:@"立即签到+"] forState:UIControlStateNormal];
        [_signButton addTarget:self action:@selector(signTap) forControlEvents:UIControlEventTouchUpInside];
        _signButton.titleLabel.font = FontMedium(15);
        [showView addSubview:_signButton];
 
        
//        _adView = [[UIView alloc]init];
//        _adView.frame = CGRectMake(0, KScreen_H-45, KScreen_W, 45);
//        [self addSubview:_adView];
//        KWeakSelf
//        [CSJADLoading share].BannerAdsLoadFish = ^(id  _Nullable bannerView) {
//            [weakSelf.adView addSubview:bannerView];
//    //        [bannerView mas_makeConstraints:^(MASConstraintMaker *make) {
//    //            make.edges.equalTo(weakSelf.adView);
//    //        }];
//            [UIView animateWithDuration:0.3 animations:^{
//                [weakSelf layoutIfNeeded];
//            }];
//        };
//        [CSJADLoading ADBannerAdRequest:[Tools currentActiveController]];
    }
    return self;
}

- (void)setDataDic:(NSDictionary *)dataDic{
    _dataDic = dataDic;
    _dataSource = dataDic[@"list"];
    if([dataDic[@"today_sign"] intValue] == 1){
        for (int i=0; i<_dataSource.count; i++) {
            if([_dataSource[i][@"is_today"] intValue] == 1){
                if([_dataSource[i][@"sign_in"] intValue] == 1){
                    self.mingtianDay = i+1;
                    [self seeAdVideo];
                    break;
                }else{
                    [_signButton setTitle:[NSString stringWithFormat:@"立即签到 +%@",_dataSource[i][@"integration"]] forState:UIControlStateNormal];
                }
            }
        }
    }
    signDayLabel.text = [NSString stringWithFormat:@"连续签到%@天",dataDic[@"sign_in_days"]];
    [_collectionView reloadData];
    if(self.isSignRequest){
        [SVProgressHUDTool dismiss];
        [YBMsgPop showPop:@"签到成功"];
    }
}
- (void)seeAdVideo{
    NSString * timestr = [Tools getNowDayTimeTimestamp];
    NSDictionary * num_dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"hongbao_sign_number"];
    if(num_dic == nil){
        NSMutableDictionary * sum_dic = [NSMutableDictionary new];
        sum_dic[@"number"] = @(0);
        sum_dic[timestr] = @{@"number":@(0),@"money":@(1022)};
        [[NSUserDefaults standardUserDefaults] setValue:sum_dic forKey:@"hongbao_sign_number"];
        [_signButton setTitle:@"看广告视频再得1022金币" forState:UIControlStateNormal];
    }else{
        if([[num_dic allKeys] containsObject:timestr]){
            if([num_dic[timestr][@"number"] intValue] == 3){
                [_signButton setTitle:[NSString stringWithFormat:@"明日签到 +%@",_dataSource[self.mingtianDay][@"integration"]] forState:UIControlStateNormal];
                _signButton.userInteractionEnabled = NO;
                _signButton.alpha = 0.66;
            }else{
                [_signButton setTitle:[NSString stringWithFormat:@"看广告视频再得%@金币",num_dic[timestr][@"money"]] forState:UIControlStateNormal];
                _signButton.tag =[num_dic[timestr][@"money"] intValue];
            }
        }else{
            NSMutableDictionary * sum_dic = [[NSMutableDictionary alloc]initWithDictionary:num_dic];
            int money = 1022-([sum_dic[@"number"] intValue]%3)*180;
            money = money-[sum_dic[timestr][@"number"] intValue]*200;
            int x = arc4random() % 100;
            if(money < 500){
                money = 500;
            }
            money=money+x;
            sum_dic[timestr] = @{@"number":@(0),@"money":@(money)};
            [[NSUserDefaults standardUserDefaults] setValue:sum_dic forKey:@"hongbao_sign_number"];
            _signButton.tag = money;
            [_signButton setTitle:[NSString stringWithFormat:@"看广告视频再得%d金币",money] forState:UIControlStateNormal];
        }
    }
}
-(void)showTextViewNavViewTo1{
    NSMutableArray * showTextViewNavViewNameArrTo1 = [NSMutableArray new];
    UIView * showTextViewNavViewViewTo1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    showTextViewNavViewViewTo1.clipsToBounds = YES;
    [self addSubview:showTextViewNavViewViewTo1];
    UILabel * showTextViewNavViewLabelTo1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    NSInteger showTextViewNavViewNumberTo1 = arc4random_uniform(20);
    NSLog(@"：%ld", (long)showTextViewNavViewNumberTo1);
    showTextViewNavViewLabelTo1.text = [NSString stringWithFormat:@"%ld",(long)showTextViewNavViewNumberTo1];
    [showTextViewNavViewLabelTo1 addSubview:showTextViewNavViewLabelTo1];
    
    UIImageView * showTextViewNavViewLabelTo2 = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
    [showTextViewNavViewViewTo1 addSubview:showTextViewNavViewLabelTo2];
    
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (self.dataSource.count) {
        return self.dataSource.count;
    }
    return 0;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    QZHomeBaoSignCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QZHomeBaoSignCell" forIndexPath:indexPath];
    if(indexPath.row < 4){
        [cell cellIndexPathRow:indexPath.row nextRow:self.mingtianDay Confin:self.dataSource[indexPath.row]];
    }else{
        NSInteger index = 8-indexPath.row+3;
        if(indexPath.row == self.mingtianDay){
            [cell cellIndexPathRow:indexPath.row nextRow:indexPath.row Confin:self.dataSource[index]];
        }else{
            [cell cellIndexPathRow:indexPath.row nextRow:self.mingtianDay Confin:self.dataSource[index]];
        }
        
    }
    
//
//    QZHomeBaoSignModel *model = [self.dataSource objectAtIndex:[index intValue]];
//    [cell cellIndexPathRow:indexPath.row rowCount:self.dataSource.count-1];
//    cell.dataModel = model;
//    cell.statueModel = model;
    return cell;

}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row < 4){
        return CGSizeMake(60, 85);
    }else{
        return CGSizeMake(60, 65);
    }
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
}

- (void)closeTap{
    [self removeFromSuperview];
}
- (void)signAdSeeFish:(void (^)(bool isfish))successBlock{
    KMutableDic(sendDic)
    sendDic[@"num"] = @(_signButton.tag);
    KWeakSelf
    [SVProgressHUDTool showWithStatus:nil];
    [YBNetBase signAndWatchAdvRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
        [SVProgressHUDTool dismiss];
        if(code == 200){
            successBlock(YES);
            NSString * timestr = [Tools getNowDayTimeTimestamp];
            NSDictionary * num_dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"hongbao_sign_number"];
            NSMutableDictionary * sum_dic = [[NSMutableDictionary alloc]initWithDictionary:num_dic];
            sum_dic[@"number"] = @([sum_dic[@"number"] intValue]+1);
            if(![[sum_dic allKeys] containsObject:timestr]){
                sum_dic[timestr] = @{@"number":@(-1),@"money":@(0)};
            }
            int money = 1022-([sum_dic[@"number"] intValue]%3)*180;
            money = money-[sum_dic[timestr][@"number"] intValue]*200;
            int x = arc4random() % 100;
            if(money < 500){
                money = 500;
            }
            money=money+x;
            
            sum_dic[timestr] = @{@"number":@([sum_dic[timestr][@"number"] intValue]+1),@"money":@(money)};
            [[NSUserDefaults standardUserDefaults] setValue:sum_dic forKey:@"hongbao_sign_number"];
            [weakSelf seeAdVideo];
        }else{
            successBlock(NO);
            [YBMsgPop showPop:msg];
        }
    }];
}
- (void)signTap{
    if(self.signButton.tag > 0){
        self.QZHongBaoSignSeeAdBlock();
    }else{
        KMutableDic(sendDic)
        KWeakSelf
        [SVProgressHUDTool showWithStatus:nil];
        [YBNetBase integrationWalletListdoSignInRequest:sendDic successBlock:^(NSDictionary * _Nonnull data, NSInteger code, NSString * _Nonnull msg) {
            if(code == 200){
                weakSelf.isSignRequest = YES;
                weakSelf.QZHongBaoSignBlock();
                weakSelf.signButton.tag = 1;
            }else{
                [YBMsgPop showPop:msg];
            }
        }];
    }
}
@end
